package com.fingard.dsp.core.specialdeal;

import java.io.Serializable;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fingard.FGBiz;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.core.common.constants.DSPCacheData;
import com.fingard.dsp.core.dao.directchannel.DirectChannelMapper;
import com.fingard.dsp.core.dao.transresult.DirectchannelTransResultMapper;
import com.fingard.dsp.entity.enums.ENDSPTranResult;
import com.fingard.dsp.entity.enums.ENDSPTransType;
import com.fingard.dsp.entity.po.DirectChannelPO;
import com.fingard.dsp.entity.po.DirectChannelTransResultPO;
import com.fingard.luna.framework.crm.cache.annotation.CachePut;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Data
@Component
public class PayStateHelper implements Serializable {

    private static final long serialVersionUID = 1L;

    public String bankCode = "";
    public String transType = "";
    public String respInfo = "";
    public String bankTransState = "";
    public String bankPayInfoCode = "";
    public String payState = "5";
    public String payInfo = "";

    private static String[] payTransType = new String[]{"1908", "1908BAT"};
    private static String[] payQueryTransType = new String[]{"1944", "1944BAT"};
    private static String singlePayFlag = "1908";
    private static String singlePayQueryFlag = "1944";

    public PayStateHelper() {
    }

    public PayStateHelper(String p_bankCode, String p_transType, String p_respInfo, String p_bankTransState, String p_bankPayInfoCode) {
        bankCode = p_bankCode;
        transType = p_transType;
        respInfo = p_respInfo;
        bankTransState = p_bankTransState;
        bankPayInfoCode = p_bankPayInfoCode;
    }

    private static String queryPayState = "select pay_stt paystate  from tbl_dspcl_pay_stt_cfg where bank_code=? and trans_type=? and trans_state=?";

    private static String queryPayInfo = "select pay_info payinfo from tbl_dspcl_pay_stt_cfg where bank_code=? and trans_type=? and trans_state=? and info_code=?";

    public void toBank() throws Exception {
        Connection dbConn = null;
        try {
            dbConn = FGBiz.dspCenter.dspSet.dbConnectionSet.getConnection();

            String tmpTransType = this.transType;
            if (Arrays.asList(payTransType).contains(this.transType)) {
                tmpTransType = singlePayFlag;
            } else if (Arrays.asList(payQueryTransType).contains(this.transType)) {
                tmpTransType = singlePayQueryFlag;
            }
            Map<String, Object> payStateMap = JDBCHelper.execQuerySingle(dbConn, queryPayState, new Object[]{this.bankCode, tmpTransType, this.bankTransState});
            if (payStateMap != null && payStateMap.size() > 0) {
                this.payState = String.valueOf(payStateMap.get("paystate"));
            }

            Map<String, Object> payInfoMap = JDBCHelper.execQuerySingle(dbConn, queryPayInfo, new Object[]{this.bankCode, tmpTransType, this.bankTransState, this.bankPayInfoCode});
            if (payInfoMap != null && payInfoMap.size() > 0) {
                String payInfo = String.valueOf(payInfoMap.get("payinfo"));
                this.payInfo = this.bankPayInfoCode != null && this.bankPayInfoCode.length() > 0 ? this.bankPayInfoCode + "-" + payInfo : payInfo;
            } else {
                this.payInfo = this.bankPayInfoCode != null && this.bankPayInfoCode.length() > 0 ? this.respInfo != null && this.respInfo.length() > 0 ? this.bankPayInfoCode + "-" + this.respInfo : this.bankPayInfoCode : this.respInfo;
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (dbConn != null) {
                dbConn.close();
            }
        }
    }

    @Autowired
    DirectchannelTransResultMapper transResultMapper;
    @Autowired
    DirectChannelMapper directChannelMapper;

    /**
     * 从缓存中查找交易结果
     * @param p_bankCode
     * @param p_transType
     * @param transState
     * @param payInfoCode
     * @return
     */
    @CachePut(code = DSPCacheData.DIRECTCAHNNELTRANSRESULT_DSP)
    public PayStateHelper convertTransResult(String p_bankCode, String p_transType, String transState, String payInfoCode) {
        DirectChannelPO directChannelPO = directChannelMapper.selectOne(new QueryWrapper<DirectChannelPO>().eq("CODE", p_bankCode));
        String packetType = ENDSPTransType.getPacketType(p_transType);
        String directPayWay = ENDSPTransType.getDirectPayWay(p_transType);
        QueryWrapper<DirectChannelTransResultPO> wrapper = new QueryWrapper<DirectChannelTransResultPO>()
                .eq("DIRECTCHANNELID", directChannelPO.getUrid())
                .eq("DIRECTPAYWAY", directPayWay)
                .eq("PACKETTYPE", packetType)
                .eq("BANKRESPCODE", transState)
                .eq("BANKRESPINFOCODE", payInfoCode)
                ;
        DirectChannelTransResultPO transResultPO = transResultMapper.selectOne(wrapper);
        if (transResultPO == null) {
            this.payState = ENDSPTranResult.PAY_STATE_UNKONWN.code();
            this.payInfo = ENDSPTranResult.PAY_STATE_UNKONWN.msg();
        } else {
            this.payState = ENDSPTranResult.getPayState(transResultPO.getPaystate());
            this.payInfo = transResultPO.getBankrespinfo();
        }
        PayStateHelper payStateHelper = new PayStateHelper();
        payStateHelper.setPayState(this.payState);
        payStateHelper.setPayInfo(this.payInfo);
        return payStateHelper;
    }
}
